<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */
namespace Plugin\Attractions\AttractionManagement\Request\Attraction_info;

use Hyperf\Validation\Request\FormRequest;

/**
 * 信息资讯表验证数据类
 */
class AttractionInfoRequest extends FormRequest
{

    public function authorize(): bool
    {
        return true;
    }


    /**
     * 新增数据验证规则
     * return array
     */
    public function rules(): array
    {
        return [
            //标题 验证
            'title' => 'required|string|max:255',
            //内容 验证
            'intro' => 'nullable|string',
            //信息分组1活动2优惠3交通 验证
            'group' => 'required',
            //类型id 验证
            'type_id' => 'required',
            //点位id 验证
            'spot_id' => 'nullable|integer',
            //封面id 验证
            'cover_id' => 'nullable|string',
            //是否显示状态1正常2停止3时间字段决定,优先级高于时间控制 验证
            'status' => 'required',
            //排序 验证
            'sort' => 'required',
            //开始时间 验证
            'start_time' => 'required',
            //结束时间 验证
            'end_time' => 'required',
            //扩展信息 验证
            'extension' => 'required',

        ];
    }


    /**
     * 字段映射名称
     * return array
     */
    public function attributes(): array
    {
        return [
            'id' => '',
            'title' => '标题',
            'group' => '信息分组1活动2优惠3交通',
            'type_id' => '类型id',
            'status' => '是否显示状态1正常2停止3时间字段决定,优先级高于时间控制',
            'sort' => '排序',
            'start_time' => '开始时间',
            'end_time' => '结束时间',
            'extension' => '扩展信息',

        ];
    }

public function messages(): array
{
    return [
            'id.required' => '必填',
            'title.required' => '必填标题',
            'group.required' => '必填信息分组1活动2优惠3交通',
            'type_id.required' => '必填类型id',
            'status.required' => '必填是否显示状态1正常2停止3时间字段决定,优先级高于时间控制',
            'sort.required' => '必填排序',
            'start_time.required' => '必填开始时间',
            'end_time.required' => '必填结束时间',
            'extension.required' => '必填扩展信息',

    ];
}

    /**
     * 获取处理后的数据
     */
    public function validated(?string $key = null, mixed $default = null): array
    {
        $data = parent::validated();
        
        // 获取所有POST数据，包括未验证的字段
        $allData = $this->post();
        
        // 合并必要的字段
        $data = array_merge($data, array_intersect_key($allData, array_flip([
            'title', 'intro', 'group', 'type_id', 'spot_id', 'cover_id', 'status', 'sort', 'start_time', 'end_time', 'extension'
        ])));
        
        // 转换日期格式
        if (isset($data['start_time'])) {
            $data['start_time'] = $this->convertDateFormat($data['start_time']);
        }
        
        if (isset($data['end_time'])) {
            $data['end_time'] = $this->convertDateFormat($data['end_time']);
        }
        
        return $data;
    }
    
    /**
     * 将ISO 8601格式转换为MySQL datetime格式
     */
    private function convertDateFormat(string $dateTime): string
    {
        try {
            $date = new \DateTime($dateTime);
            return $date->format('Y-m-d H:i:s');
        } catch (\Exception $e) {
            return $dateTime; // 如果转换失败，返回原始值
        }
    }
}